/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.basic.park.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.basic.park.dto.BaseShiftQueueExcelDTO;
import com.hopes.bizz.basic.park.entity.BaseShiftEmployeeEntity;
import com.hopes.bizz.basic.park.entity.BaseShiftQueueEntity;
import com.hopes.bizz.basic.park.entity.BaseShiftQueueRecordEntity;
import com.hopes.bizz.basic.park.service.BaseShiftEmployeeService;
import com.hopes.bizz.basic.park.service.BaseShiftQueueRecordService;
import com.hopes.bizz.basic.park.service.BaseShiftQueueService;
import com.hopes.bizz.basic.park.vo.BaseShiftQueueExcelVO;
import com.hopes.bizz.basic.park.vo.BaseShiftQueueVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 值班排班管理
 *
 * @author pig
 * @date 2024-01-23 13:30:38
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/queue")
@Tag(description = "queue", name = "值班排班管理管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BaseShiftQueueController {

    private final BaseShiftQueueService baseShiftQueueService;
    private final BaseShiftQueueRecordService baseShiftQueueRecordService;
    private final BaseShiftEmployeeService baseShiftEmployeeService;

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param baseShiftQueue 值班排班管理
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('park_queue_view')")
    public R getbaseShiftQueuePage(Page<BaseShiftQueueEntity> page, BaseShiftQueueEntity baseShiftQueue) {
        LambdaQueryWrapper<BaseShiftQueueEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(baseShiftQueue.getShiftDate()), BaseShiftQueueEntity::getShiftDate, baseShiftQueue.getShiftDate());
        wrapper.eq(ObjectUtil.isNotNull(baseShiftQueue.getShiftUserId()), BaseShiftQueueEntity::getShiftUserId, baseShiftQueue.getShiftUserId());
        wrapper.eq(ObjectUtil.isNotNull(baseShiftQueue.getShiftDate()), BaseShiftQueueEntity::getShiftDate, baseShiftQueue.getShiftDate());
        return R.ok(baseShiftQueueService.page(page, wrapper).convert((item) -> {
            BaseShiftQueueVO baseShiftQueueVO = new BaseShiftQueueVO();
            BeanUtils.copyProperties(item, baseShiftQueueVO);
            baseShiftQueueVO.setQueueRecordCount(baseShiftQueueRecordService.lambdaQuery().eq(BaseShiftQueueRecordEntity::getShiftQueueId, item.getId()).count());
            return baseShiftQueueVO;
        }));
    }


    /**
     * 通过id查询值班排班管理
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('park_queue_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(baseShiftQueueService.getById(id));
    }

    /**
     * 新增值班排班管理
     *
     * @param baseShiftQueue 值班排班管理
     * @return R
     */
    @Operation(summary = "新增值班排班管理", description = "新增值班排班管理")
    @SysLog("新增值班排班管理")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('park_queue_add')")
    public R save(@RequestBody BaseShiftQueueEntity baseShiftQueue) {
        Long shiftUserId = baseShiftQueue.getShiftUserId();
        Long shiftWorkId = baseShiftQueue.getShiftWorkId();
        Date shiftDate = baseShiftQueue.getShiftDate();
        BaseShiftQueueEntity one = baseShiftQueueService.lambdaQuery()
                .eq(BaseShiftQueueEntity::getShiftUserId, shiftUserId)
                .eq(BaseShiftQueueEntity::getShiftDate, shiftDate)
                .eq(BaseShiftQueueEntity::getShiftWorkId, shiftWorkId).one();
        if (ObjectUtil.isNotNull(one)){
            return R.failed("重复数据");
        }
        BaseShiftEmployeeEntity employeeEntity = baseShiftEmployeeService.getById(shiftUserId);
        baseShiftQueue.setPhone(employeeEntity.getPhone());
        baseShiftQueue.setPost(employeeEntity.getPost());
        return R.ok(baseShiftQueueService.save(baseShiftQueue));
    }

    /**
     * 新增值班排班管理
     *
     * @param baseShiftQueueList 值班排班管理
     * @return R
     */
    @Operation(summary = "新增值班排班管理", description = "新增值班排班管理")
    @SysLog("新增值班排班管理")
    @PostMapping("/saveBatch")
    @PreAuthorize("@pms.hasPermission('park_queue_add')")
    public R save(@RequestBody List<BaseShiftQueueEntity> baseShiftQueueList) {
        return R.ok(baseShiftQueueService.saveBatch(baseShiftQueueList));
    }

    /**
     * 修改值班排班管理
     *
     * @param baseShiftQueue 值班排班管理
     * @return R
     */
    @Operation(summary = "修改值班排班管理", description = "修改值班排班管理")
    @SysLog("修改值班排班管理")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('park_queue_edit')")
    public R updateById(@RequestBody BaseShiftQueueEntity baseShiftQueue) {
        return R.ok(baseShiftQueueService.updateById(baseShiftQueue));
    }

    /**
     * 通过id删除值班排班管理
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除值班排班管理", description = "通过id删除值班排班管理")
    @SysLog("通过id删除值班排班管理")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('park_queue_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(baseShiftQueueService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('park_queue_export')")
    public List<BaseShiftQueueExcelVO> template() {
        return baseShiftQueueService.exportExcel(Collections.singletonList(new BaseShiftQueueEntity()));
    }

    /**
     * 导出excel 表格
     *
     * @param baseShiftQueueExcelDto 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('park_queue_export')")
    public List<BaseShiftQueueExcelVO> export(BaseShiftQueueExcelDTO baseShiftQueueExcelDto) {
        return baseShiftQueueService.exportExcel(baseShiftQueueService.list());
    }

    /**
     * 导入excel 表格
     *
     * @param baseShiftQueueExcelDtoList 查询条件
     * @param bindingResult              错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('park_queue_export')")
    public R importExcel(@RequestExcel List<BaseShiftQueueExcelDTO> baseShiftQueueExcelDtoList, BindingResult bindingResult) {
        return baseShiftQueueService.importExcel(baseShiftQueueExcelDtoList, bindingResult);
    }
}
